<?php
    include '../lib/confs/Conf.php';    
    //Correct leave data
    correct(2014, "LTY001");
    echo "Update successful";
    exit;
    function update($year,$type,$empId,$days) {        
        $query = "UPDATE hs_hr_employee_leave_quota 
                  SET leave_taken = ".$days."
                  WHERE leave_type_id ='".$type."' AND year =".$year."
                  AND `employee_id` = '".$empId."'";        
        return  query($query);
    }
    
    function correct($year,$type){
        $takenRecords = get($year, $type);
        while ($takenRecord = mysql_fetch_array($takenRecords)) {
            update($year,$type,$takenRecord['employee_id'],$takenRecord['takens']);
        }
    }
    
    function get($year,$type) {        
        $query = "SELECT employee_id , sum( leave_length_days ) AS 'takens'
                  FROM hs_hr_leave WHERE YEAR( `leave_date` ) = ".$year." AND `leave_status` = 3
                  AND leave_type_id LIKE '".$type."'
                  GROUP BY employee_id ";
        return query($query);
    }
    
    function query($query) {
        $conf = new Conf();    
        $user=$conf->dbuser;
        $password=$conf->dbpass;
        $database=$conf->dbname;
        $server=$conf->dbhost;
	$con = mysql_connect($server, $user, $password);
        if (!$con) {
            die('Could not connect: ' . mysql_error());
        }
        @mysql_select_db($database) or die("Unable to select database " . $database . ".");      
        $result = mysql_query($query) or die(mysql_error());        
        mysql_close();
        return $result;
    }

?>